package com.sojson.util.agent;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sojson.util.StringUtil;
import com.sojson.util.text.Convert;

/**
 * 客户端工具类
 *
 * @author lyh
 */
public class ServletUtil {

    /**
     * 获取请求对象
     * 
     * @return
     */
    public static ServletRequest getServletRequest() {
        return getHttpServletRequest();
        // return ((WebSubject)SecurityUtils.getSubject()).getServletRequest();
    }

    /**
     * 获取响应对象
     * 
     * @return
     */
    public static ServletResponse getServletResponse() {
        return getHttpServletResponse();
        // return ((WebSubject)SecurityUtils.getSubject()).getServletResponse();
    }

    /**
     * 获取请求对象
     * 
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取响应对象
     * 
     * @return
     */
    public static HttpServletResponse getHttpServletResponse() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获取String参数
     * 
     * @param key
     * @return
     */
    public static String getParameter(String key) {
        return getHttpServletRequest().getParameter(key);
    }

    /**
     * 获取String参数(给一个默认值防止转换失败,空指针???)
     * 
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public static String getParameter(String key, String defaultValue) {
        return toStr(getParameter(key), defaultValue);
    }

    /**
     * 获取Integer参数
     * 
     * @param key
     * @return
     */
    public static Integer getParameterToInt(String key) {
        return toInt(getParameter(key));
    }

    /**
     * 获取Integer参数(给一个默认值防止转换失败,空指针???)
     * 
     * @param key
     * @param defaultValue 默认值
     * @return
     */
    public static Integer getParameterToInt(String key, Integer defaultValue) {
        return toInt(getParameter(key), defaultValue);
    }

    /**
     * 把值放入到当前的Request里
     * 
     * @param key
     * @param value
     */
    public static void setValRequest(String key, Object value) {
        getHttpServletRequest().setAttribute(key, value);
    }

    /**
     * 从当前的Request里取值
     * 
     * @param key
     * @return
     */
    public static Object getValRequest(String key) {
        return getHttpServletRequest().getAttribute(key);
    }

    /**
     * 删除Request里的值
     * 
     * @param key
     */
    public static void removeValRequest(String key) {
        getHttpServletRequest().removeAttribute(key);
    }

    /**
     * 从当前的请求头里取值
     * 
     * @param key
     * @return
     */
    public static String getReqHeader(String key) {
        return getHttpServletRequest().getHeader(key);
    }

    /**
     * 把值放入到响应头中
     * 
     * @param key
     * @param value
     */
    public static void setValHeader(String key, String value) {
        getHttpServletResponse().setHeader(key, value);
    }

    /**
     * 把值放入到响应头中
     * 
     * @param key
     * @return
     */
    public static String getResHeader(String key) {
        return getHttpServletResponse().getHeader(key);
    }

    /**
     * 获取客户端访问的地址
     * 
     * @return
     */
    public static String getRequestUri() {
        return getRequestUri(getHttpServletRequest());
    }

    /**
     * 获取客户端访问的地址
     * 
     * @param request
     * @return
     */
    public static String getRequestUri(HttpServletRequest request) {
        return request.getRequestURI();
    }

    /**
     * 获取HttpSession
     * 
     * @return
     */
    public static HttpSession getHttpSession() {
        return getHttpServletRequest().getSession();
    }

    /**
     * 是否是Ajax异步请求
     *
     * @param request
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1) {
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
            return true;
        }

        String uri = request.getRequestURI();
        if (inStringIgnoreCase(uri, ".json", ".xml")) {
            return true;
        }

        String ajax = request.getParameter("__ajax");
        return inStringIgnoreCase(ajax, "json", "xml");
    }

    /**
     * 判断一个对象是否不是空(空对象)
     * 
     * @param obj
     * @return
     */
    private static boolean inStringIgnoreCase(String str, String... strs) {
        return StringUtil.inStringIgnoreCase(str, strs);
    }

    /**
     * 转换为int<br>
     * 如果给定的值为空，或者转换失败，返回默认值<br>
     * 转换失败不会报错
     *
     * @param value        被转换的值
     * @param defaultValue 转换错误时的默认值
     * @return 结果
     */
    private static Integer toInt(Object value, Integer defaultValue) {
        return Convert.toInt(value, defaultValue);
    }

    /**
     * 转换为int<br>
     * 如果给定的值为<code>null</code>，或者转换失败，返回默认值<code>null</code><br>
     * 转换失败不会报错
     *
     * @param value 被转换的值
     * @return 结果
     */
    private static Integer toInt(Object value) {
        return Convert.toInt(value);
    }

    /**
     * 转换为字符串<br>
     * 如果给定的值为null，或者转换失败，返回默认值<br>
     * 转换失败不会报错
     *
     * @param value        被转换的值
     * @param defaultValue 转换错误时的默认值
     * @return 结果
     */
    private static String toStr(Object value, String defaultValue) {
        return Convert.toStr(value, defaultValue);
    }

}
